package com.lark.web.controller;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.lark.common.annotation.Log;
import com.lark.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.lark.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lark.common.enums.BusinessType;
import com.lark.common.utils.poi.ExcelUtil;
import com.lark.framework.cache.DictUtils;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.lark.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.lark.web.domain.Host;
import com.lark.web.service.HostService;

/**
 * 主机Controller
 * @author fei
 * @email 952597665@qq.com
 * @date 2024-08-10
 */
@RestController
@RequestMapping("/dockerinsight/host")
public class HostController extends BaseController {

    @Autowired
    private HostService hostService;

    /**
     * 查询可用的主机列表
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:list')")
    @GetMapping("/getEnalbeList")
    public R<List<Host>> getEnalbeList(@RequestParam(value = "ip", required = false) String ip) {
        return R.data(hostService.getEnalbeList(ip));
    }

    /**
     * 查询主机列表
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Host host, HttpServletRequest request, HttpServletResponse response) {
        host.setPage(new PageDomain(request, response));
        return R.data(hostService.findPage(host));
    }

    /**
     * 获取主机详细信息
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:query')")
    @GetMapping(value = "/{id}")
    public R<Host> detail(@PathVariable("id") String id) {
        return R.data(hostService.get(id));
    }

    /**
     * 新增主机
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:add')")
    @Log(title = "主机", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  Host host) {
        return R.status(hostService.save(host));
    }

    /**
     * 修改主机
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:edit')")
    @Log(title = "主机", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Host host) {
        return R.status(hostService.save(host));
    }

    /**
     * 删除主机
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:remove')")
    @Log(title = "主机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(hostService.deleteHostByIds(ids));
    }


    /**
     * 校验主机IP的唯一性
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:query')")
    @Log(title = "主机", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkHostIpUnique/{hostIp}/{id}", "/checkHostIpUnique/{hostIp}"})
    public R checkHostIpUnique(@NotBlank(message = "主机IP不允许为空") @PathVariable("hostIp") String hostIp, @PathVariable(value = "id", required = false) String id) {
        Host host = new Host();
        host.setHostIp(hostIp);
        host.setId(id);
        hostService.checkHostIpUnique(host);
        return R.status(Boolean.TRUE);
    }

    /**
     * 导出主机列表
     */
    @PreAuthorize("@ss.hasPermi('dockerinsight:host:export')")
    @Log(title = "主机", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Host host) {
        List<Host> list = hostService.findList(host);
        ExcelUtil<Host> util = new ExcelUtil<Host>(Host.class);
        return util.exportExcel(list, "主机数据");
    }
	
    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
